package net.mingsoft.basic.util;

import java.io.*;
import java.util.regex.*;
import cn.hutool.json.*;
import java.util.*;
import net.mingsoft.base.entity.*;
import org.springframework.core.io.*;
import java.nio.charset.*;
import org.apache.commons.io.*;

public class FileUtil
{
    private static String fileSuffix;
    
    public static void del(final String json) {
        final Pattern pattern = Pattern.compile(FileUtil.fileSuffix);
        final Matcher matcher = pattern.matcher(json);
        while (matcher.find()) {
            try {
                FileUtils.forceDelete(new File(BasicUtil.getRealPath(matcher.group())));
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void del(final List<?> list) {
        String json = "";
        for (final Object entity : list) {
            json = JSONUtil.toJsonStr(entity);
            del(json);
        }
    }
    
    public static void del(final BaseEntity entity) {
        final String json = JSONUtil.toJsonStr((Object)entity);
        del(json);
    }
    
    public static String readJarFile(final String path) {
        try {
            final ClassPathResource classPathResource = new ClassPathResource(path);
            return IOUtils.toString(classPathResource.getInputStream(), StandardCharsets.UTF_8);
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    
    static {
        FileUtil.fileSuffix = "[/|\\\\]upload.*?\\.(rmvb|mpga|mpg4|mpeg|docx|xlsx|pptx|jpeg|[a-z]{3})";
    }
}
